<?php
class Tourismtemplet extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->library('pagination');
        $this->load->model('tourism_freight_template_model', 'tourism_freight_template');
        $this->load->model('tourism_freight_price_list_model', 'tourism_freight_price_list');
    }

    /**
     *运费模板列表
     */
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $getData['uid'] = $this->uid;
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismgoods/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismgoods/search');
        $config['total_rows'] = $this->tourism_freight_template->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['list'] = $this->tourism_freight_template->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('tourismtemplet/grid', $data);
    }

    /**
     *运费模板列表
     */
    public function add()
    {
        $this->load->view('tourismtemplet/add');
    }

    /**
    *运费模板添加
    */
    public function addPost()
    {
        $template_param['name'] = $this->input->post('name', true);
        $template_param['logistics'] = (int)$this->input->post('logistics');
        $template_param['methods'] = (int)$this->input->post('methods');
        $template_param['uid'] = $this->uid;
        if ($template_param['methods'] == 1) {
            $default_params['first_unit'] = $this->input->post('piece_first_unit');
            $default_params['first_price'] = $this->input->post('piece_first_price');
            $default_params['add_unit'] = $this->input->post('piece_add_unit');
            $default_params['add_price'] = $this->input->post('piece_add_price');
        } else {
            $default_params['first_unit'] = $this->input->post('weight_first_unit');
            $default_params['first_price'] = $this->input->post('weight_first_price');
            $default_params['add_unit'] = $this->input->post('weight_add_unit');
            $default_params['add_price'] = $this->input->post('weight_add_price');
        }
        $default_params = $this->unsetEmpty($default_params);
        $default_params['area'] = '';
        if (count($default_params) < 4) {
            $this->error('tourismtemplet/add', '', '默认地址为必填项');
            exit(0);
        }
        $default_params['is_default'] = 1;
        $list_params = $this->input->post('list');
        if (!$list_params) {
            $this->error('tourismtemplet/add', '', '请添加城市并设置运费');
            exit(0);
        }
        foreach ($list_params as $key => $item) {
            $list_params[$key]['is_default'] = 0;
        }
        foreach ($list_params as $item) {
            $params[] = $item;
        }
        if (is_array($list_params)) {
            $params[] = $default_params;
        }
        $this->db->trans_start();
        $tid = $this->tourism_freight_template->addTemplate($template_param);
        foreach ($params as $key => $item) {
            $params[$key]['tid'] = $tid;
        }
        $status = $this->tourism_freight_price_list->addList($params);
        $this->db->trans_complete();

        if ($status) {
            $this->success('tourismtemplet/grid', '', '保存成功！');
        } else {
            $this->error('tourismtemplet/grid', '', '保存失败！');
        }
    }

    /**
     *运费模板修改
     */
    public function edit($tid)
    {
        //获取默认地址
        $defaultList = $this->tourism_freight_price_list->getFreight($tid, 1);
        $template = $this->tourism_freight_template->getTemplate($tid);
        $data['template'] = $template->row();
        $data['defaultList'] = $defaultList->row();
        $this->load->view('tourismtemplet/edit', $data);
    }

    public function editPost()
    {
        $id = $this->input->post('id');
        $template_param['name'] = $this->input->post('name', true);
        $template_param['logistics'] = (int)$this->input->post('logistics');
        $template_param['methods'] = (int)$this->input->post('methods');
        $template_param['uid'] = $this->uid;
        if ($template_param['methods'] == 1) {
            $default_params['first_unit'] = $this->input->post('piece_first_unit');
            $default_params['first_price'] = $this->input->post('piece_first_price');
            $default_params['add_unit'] = $this->input->post('piece_add_unit');
            $default_params['add_price'] = $this->input->post('piece_add_price');
        } else {
            $default_params['first_unit'] = $this->input->post('weight_first_unit');
            $default_params['first_price'] = $this->input->post('weight_first_price');
            $default_params['add_unit'] = $this->input->post('weight_add_unit');
            $default_params['add_price'] = $this->input->post('weight_add_price');
        }

        $default_params = $this->unsetEmpty($default_params);
        $default_params['area'] = '';
        if (count($default_params) < 4) {
            $this->error('tourismtemplet/add', '', '默认地址为必填项');
            exit(0);
        }
        $default_params['is_default'] = 1;
        $list_params = $this->input->post('list');
        foreach ($list_params as $key => $item) {
            $list_params[$key]['is_default'] = 0;
        }
        foreach ($list_params as $item) {
            $params[] = $item;
        }
        if (is_array($list_params)) {
            $params[] = $default_params;
        }
        foreach ($params as $key => $item) {
            $params[$key]['tid'] = $id;
        }
        $this->tourism_freight_template->updateTemplate($id, $template_param);
        $this->tourism_freight_price_list->deleteList($id);
        $this->tourism_freight_price_list->addList($params);
        $this->db->trans_complete();
        $this->success('tourismtemplet/grid', '', '保存成功！');
    }

    /**
    *ajax 请求数据
    */
    public function ajax_template($tid = 0)
    {
        $result = $this->tourism_freight_price_list->getFreight($tid, 0);
        echo json_encode(array(
            'status' => 1,
            'data' => $result->result()
        ));
        exit;
    }

     /**
      * 
      * @param unknown $group
      * @return unknown
      */
    public function unsetEmpty($group = array())
    {
        foreach ($group as $key => $val) {
            if (empty($val)) {
                unset($group[$key]);
            }
        }
        return $group;
    }
}